package com.fimuz.mqmeet.api.loginuser.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fimuz.mqmeet.api.loginuser.dao.LoginuserDao;
import com.fimuz.mqmeet.api.loginuser.service.LoginuserService;
import com.fimuz.mqmeet.api.loginuser.service.model.ActiveUserEmailSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ActiveUserEmailSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ChangePasswordSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ChangePasswordSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.CreateOrUpdateSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.CreateOrUpdateSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.CreateSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.CreateSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.DeleteSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.DeleteSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ExistsEmailSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ExistsEmailSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.GetSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.GetSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ListSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.ListSrModel;
import com.fimuz.mqmeet.api.loginuser.service.model.UpdateSpModel;
import com.fimuz.mqmeet.api.loginuser.service.model.UpdateSrModel;
import com.fimuz.mqmeet.helper.exception.MsResultCode;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzAuthRoleLoginuserMapper;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzLoginuserMapper;
import com.fimuz.mqmeet.helper.mybatis.dao.HxzTokenMapper;
import com.fimuz.mqmeet.helper.mybatis.model.HxzAuthRoleLoginuser;
import com.fimuz.mqmeet.helper.mybatis.model.HxzAuthRoleLoginuserExample;
import com.fimuz.mqmeet.helper.mybatis.model.HxzLoginuser;
import com.fimuz.mqmeet.helper.mybatis.model.HxzLoginuserExample;
import com.fimuz.mqmeet.helper.mybatis.model.HxzToken;
import com.fimuz.mqmeet.helper.mybatis.model.HxzTokenExample;
import com.fimuz.mqmeet.helper.utils.PasswordUtils;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;
import com.fosun.spr.core.utils.SprDaoUtils;

@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class LoginuserServiceImpl extends CoBaseService implements LoginuserService {
	@Resource
	private LoginuserDao loginuserDao;
	@Resource
	private HxzLoginuserMapper hxzLoginuserMapper;
	@Resource
	private HxzAuthRoleLoginuserMapper hxzAuthRoleLoginuserMapper;
	@Resource
	private HxzTokenMapper hxzTokenMapper;
 
	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(loginuserDao.list(spModel));
		srModel.setTotalCount(loginuserDao.listCount(spModel));
		return srModel;
	}	
	@Override
	public ExistsEmailSrModel existsEmail(ExistsEmailSpModel spModel) {
		ExtAsserts.notNull(spModel.getUserEmail(),"userEmail");
		ExistsEmailSrModel srModel = new ExistsEmailSrModel();
		HxzLoginuserExample example = new HxzLoginuserExample();
		HxzLoginuserExample.Criteria c = example.createCriteria();
		c.andIsDeletedEqualTo(IS_DELETED_N);
		c.andUserEmailEqualTo(spModel.getUserEmail().toLowerCase());
		List<HxzLoginuser> list = hxzLoginuserMapper.selectByExample(example);
		if(list!=null && list.size()>0){
			srModel.setIsExists(ExistsEmailSrModel.ISEXISTS_Y);
		}else{
			srModel.setIsExists(ExistsEmailSrModel.ISEXISTS_N);
		}
		return srModel;
	}	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		SprDaoUtils.delete(hxzLoginuserMapper, spModel.getId());
		return srModel;
	}	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//校验角色是否存在
		if(spModel.getRoleUuids()!=null){
			for(int i=0,j=spModel.getRoleUuids().length;i<j;i++){
				//TODO 校验角色是否存在
			}
		}
		//人员新增或修改
		if(true){			
			HxzLoginuser db = new HxzLoginuser();
			cglibCopyProperties(spModel, db, true);
			if(StringUtils.isBlank(spModel.getId())){
				ExtAsserts.notNull(spModel.getLoginPassword(), "loginPassword");
				//校验帐号是否存在
				HxzLoginuserExample example = new HxzLoginuserExample();
				HxzLoginuserExample.Criteria c = example.createCriteria();
				c.andIsDeletedEqualTo(IS_DELETED_N);
				c.andUserEmailEqualTo(spModel.getUserEmail());
				List<HxzLoginuser> list = hxzLoginuserMapper.selectByExample(example);
				if(list.size()>0){
					throw new ServiceException("-1","邮箱已经存在");
				}
				db.setTeamId(spModel.getSessionTeamId());
				db.setLoginPassword(PasswordUtils.mi(spModel.getLoginPassword()));
				SprDaoUtils.insert(hxzLoginuserMapper, db);
				srModel.setId(db.getId());
			}else{
				SprDaoUtils.updateByPrimaryKey(hxzLoginuserMapper, db);
				srModel.setId(db.getId());
			}
		}
		//调整角色
		if(spModel.getRoleUuids()!=null){
			HxzAuthRoleLoginuserExample example = new HxzAuthRoleLoginuserExample();
			HxzAuthRoleLoginuserExample.Criteria c = example.createCriteria();
			c.andLoginuserIdEqualTo(srModel.getId());
			hxzAuthRoleLoginuserMapper.deleteByExample(example);
			
			for(int i=0,j=spModel.getRoleUuids().length;i<j;i++){
				HxzAuthRoleLoginuser db = new HxzAuthRoleLoginuser();
				db.setRoleId(spModel.getRoleUuids()[i]);
				SprDaoUtils.insert(hxzAuthRoleLoginuserMapper, db);
			}
		}

		return srModel;
	}	

	
	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		HxzLoginuser db = (HxzLoginuser)SprDaoUtils.getById(hxzLoginuserMapper, spModel.getId());
		cglibCopyProperties(db, srModel, true);
		
		//获取角色ID数组
		if(true){			
			HxzAuthRoleLoginuserExample example = new HxzAuthRoleLoginuserExample();
			HxzAuthRoleLoginuserExample.Criteria c = example.createCriteria();
			c.andLoginuserIdEqualTo(srModel.getId());	
			List<HxzAuthRoleLoginuser> dbs =  hxzAuthRoleLoginuserMapper.selectByExample(example);
			//将dbs的id转为数组
			 srModel.setRoleUuids(getArrayFromList(dbs,"roleId")); //TODO
		}
		return srModel;
	}
	
	
	@Override
	public ChangePasswordSrModel changePassword(ChangePasswordSpModel spModel) {
		ExtAsserts.notNull(spModel.getOldPassword(),"oldPassword");
		ExtAsserts.notNull(spModel.getNewPassword(),"newPassword");
		ChangePasswordSrModel srModel = new ChangePasswordSrModel();

		HxzLoginuser record = (HxzLoginuser)SprDaoUtils.getById(hxzLoginuserMapper, spModel.getSessionUserId());
		ExtAsserts.notNull(record,spModel.getSessionUserId());

		if(!record.getLoginPassword().equals(PasswordUtils.mi(spModel.getOldPassword()))){
			throw new ServiceException(MsResultCode.MS10001);
		}
		record.setLoginPassword(PasswordUtils.mi(spModel.getNewPassword()));
		SprDaoUtils.updateByPrimaryKey(hxzLoginuserMapper, record);
		return srModel;
	}
	
	@Override
	public CreateSrModel create(CreateSpModel spModel) {
		ExtAsserts.notNull(spModel.getUserName(),"userName");
		ExtAsserts.notNull(spModel.getUserEmail(),"userEmail");
		ExtAsserts.notNull(spModel.getLoginPassword(), "loginPassword");
		CreateSrModel srModel = new CreateSrModel();
		HxzLoginuser db = new HxzLoginuser();
		cglibCopyProperties(spModel, db, true);

		// 校验帐号是否存在
		HxzLoginuserExample example = new HxzLoginuserExample();
		HxzLoginuserExample.Criteria c = example.createCriteria();
		c.andIsDeletedEqualTo(IS_DELETED_N);
		c.andUserEmailEqualTo(spModel.getUserEmail());
		List<HxzLoginuser> list = hxzLoginuserMapper.selectByExample(example);
		if (list.size() > 0) {
			throw new ServiceException(MsResultCode.MS10004);
		}
		db.setTeamId(spModel.getSessionTeamId());
		db.setLoginPassword(PasswordUtils.mi(spModel.getLoginPassword()));
		SprDaoUtils.insert(hxzLoginuserMapper, db);
		srModel.setId(db.getId());
		
		for(int i=0,j=spModel.getRoleUuids().length;i<j;i++){
			HxzAuthRoleLoginuser roleUser = new HxzAuthRoleLoginuser();
			roleUser.setRoleId(spModel.getRoleUuids()[i]);
			roleUser.setLoginuserId(db.getId());
			SprDaoUtils.insert(hxzAuthRoleLoginuserMapper, roleUser);
		}		
		//发送激活邮件
		return srModel;
	}	
	@Override
	public UpdateSrModel update(UpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		ExtAsserts.notNull(spModel.getUserName(),"userName");
		UpdateSrModel srModel = new UpdateSrModel();
		HxzLoginuser record = (HxzLoginuser)SprDaoUtils.getById(hxzLoginuserMapper, spModel.getId());
		record.setUserEmail(spModel.getUserName());
		record.setMemo(spModel.getMemo());
		
		//调整角色
		if(spModel.getRoleUuids()!=null){
			HxzAuthRoleLoginuserExample example = new HxzAuthRoleLoginuserExample();
			HxzAuthRoleLoginuserExample.Criteria c = example.createCriteria();
			c.andLoginuserIdEqualTo(spModel.getId());
			hxzAuthRoleLoginuserMapper.deleteByExample(example);
			
			for(int i=0,j=spModel.getRoleUuids().length;i<j;i++){
				HxzAuthRoleLoginuser db = new HxzAuthRoleLoginuser();
				db.setRoleId(spModel.getRoleUuids()[i]);
				SprDaoUtils.insert(hxzAuthRoleLoginuserMapper, db);
			}
		}
		
		return srModel;
	}	
	@Override
	public ActiveUserEmailSrModel activeUserEmail(ActiveUserEmailSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		ExtAsserts.notNull(spModel.getUserEmailToken(),"userEmailToken");
		ActiveUserEmailSrModel srModel = new ActiveUserEmailSrModel();
		
		HxzTokenExample example = new HxzTokenExample();
		HxzTokenExample.Criteria c = example.createCriteria();
		c.andIsDeletedEqualTo(IS_DELETED_N);
		c.andTokenEqualTo(spModel.getUserEmailToken());
		c.andTokenTypeEqualTo("activeUserEmail");
		HxzToken token = (HxzToken)SprDaoUtils.getRowByExample(hxzTokenMapper, example);
		
		HxzLoginuser loginuser = (HxzLoginuser)SprDaoUtils.getById(hxzLoginuserMapper, spModel.getId());
		loginuser.setActiveEmailFlag("1");
		loginuser.setUserEmail(token.getTokenValue1());
		SprDaoUtils.updateByPrimaryKey(hxzLoginuserMapper, loginuser);
		
		return srModel;
	}		
}
